define(["jquery","bbcare","oms_config/root",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js"],function($,$bb,$root,$datatableServers,$datatableUi){
	var servers = {};
	var URL = $bb.baseUrl("followup");
    /**
     * 请求患者根据姓名或者手机号码
     */
    servers.ajaxPatients = function(params,callback,dataValue){
        $bb.ajax({
            url: URL + "followup/service/patient/patientQueueListQuery",
            data:JSON.stringify({
                data : params
            }),
            success: function(data){
                if(data.success){ 
                    callback(data.data.rows,dataValue);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询患者出错！");
                }
            }
        });
    }
    /**
     * 处理根据手机号和姓名搜索出来的患者的数据
     */
    servers.handlePatientsByPhone = function(list){
        var config = $root.init();
        var arr = [];
        for (var i = 0; i < list.length; i++) {
            arr[i] = list[i];
            arr[i].sexShow = config.dom.surveyStagePatientSex;
        }
        return arr;
    }
    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "opCode":"1019",
                "opType":"2",
                "busiId":$(".panel-body table").attr("busiId"),
                "patientName": $.trim($('#searchByName').val()),
                "phoneNum": $.trim($('#searchByPhone').val()),
                "qryStartTime":$.trim($('#timeFrom').val()) + "00:00:00",
                "qryEndTime":$.trim($('#timeTo').val()) + "23:59:59"
            } 
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsData = function(){
        var columnsArr = ['patientName','phoneNum','birthday','opTime',null];
        return $datatableServers.getColumnsData(columnsArr);
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = '<div class="btn-look">查看</div>';
        $('td:last()', row).html(h);
        $("td:last() .btn-look",row).unbind("click");
        $("td:last() .btn-look",row).click(function(){
            var dataValue = {
                "busiId":data.busiId,
                "updateAccept":data.updateAccept,
                "patientId":data.patientId
            };
            var openPage;
            switch (data.busiId) {
                case "maternalPre1214":
                    openPage = "surveyStageRecord01";
                    break;
                case "maternalPre0028":
                    openPage = "surveyStageRecord02";
                    break;
                case "maternalPre2840":
                    openPage = "surveyStageRecord03";
                    break;
                case "maternalDelivery":
                    openPage = "surveyStageRecord04";
                    break;
                case "maternalPre42day":
                    openPage = "surveyStageRecord05";
                    break;
                default:
                    break;
            }
            $bb.openPage(openPage,dataValue);
        });   
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $(".panel-body table").prev("div.row").remove();
        $("#searchByName, #searchByHisNo, #searchByPhone, #timeFrom, #timeTo").bind("input propertychange",function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patientFollow/queryPtFuAssesList?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }
    
    return servers;
});